/**
 * 配置管理器模块
 * 负责系统配置的管理和更新
 */

class ConfigManager {
    constructor() {
        this.init();
    }

    init() {
        this.setupConfigToggles();
    }

    setupConfigToggles() {
        const toggles = document.querySelectorAll('.toggle-switch');
        toggles.forEach(toggle => {
            toggle.addEventListener('click', () => {
                toggle.classList.toggle('active');
                this.updateSystemConfig();
            });
        });
    }

    async updateSystemConfig() {
        const config = {};
        const toggles = document.querySelectorAll('.toggle-switch');
        toggles.forEach(toggle => {
            const configPath = toggle.getAttribute('data-config');
            const isActive = toggle.classList.contains('active');

            // 处理嵌套配置路径
            if (configPath && configPath.includes('.')) {
                const parts = configPath.split('.');
                let current = config;
                for (let i = 0; i < parts.length - 1; i++) {
                    if (!current[parts[i]]) {
                        current[parts[i]] = {};
                    }
                    current = current[parts[i]];
                }
                current[parts[parts.length - 1]] = isActive;
            } else if (configPath) {
                config[configPath] = isActive;
            }
        });

        try {
            const response = await fetch('/api/system/config', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(config)
            });

            if (response.ok) {
                window.logManager?.addLogEntry('info', '配置更新成功');
            } else {
                throw new Error('配置更新失败');
            }
        } catch (error) {
            window.logManager?.addLogEntry('error', `配置更新失败: ${error.message}`);
        }
    }

    async loadSystemConfig() {
        try {
            const response = await fetch('/api/system/config');
            const config = await response.json();
            this.applyConfigToUI(config);
        } catch (error) {
            console.error('Failed to load system config:', error);
        }
    }

    applyConfigToUI(config) {
        const toggles = document.querySelectorAll('.toggle-switch');
        toggles.forEach(toggle => {
            const configPath = toggle.getAttribute('data-config');
            if (!configPath) return;

            let value = config;
            const parts = configPath.split('.');
            
            for (const part of parts) {
                if (value && typeof value === 'object' && part in value) {
                    value = value[part];
                } else {
                    value = undefined;
                    break;
                }
            }

            if (value === true) {
                toggle.classList.add('active');
            } else {
                toggle.classList.remove('active');
            }
        });
    }
}

// 导出到全局
window.ConfigManager = ConfigManager; 